package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.common.util.FileUtils
import com.turing.data.engine.utils.RegexExtract

/**
 * 场景：资产管理合同
 * 版本：Version 1.0
 * 要素名称：运作方式2
 */

class OperateMethod2_30183_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        def Regex_1 = /.*参与和退出的开放日和时间.*/

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        def set = new HashSet()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 20 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if(expandString =~ Regex_1){
                        trueValueSet.add(drawResultVO)
                        set.add(drawResultVO.text)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        try{
            if(trueValueSet.size()==0){

                String REGEX0 = /资产管理计划的运作方式.{1,15}。/
                println dependParams.JSONTextMap.size()
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX0,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )

                if(trueValueSet.size()>0){
                    def textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)

                    def rawPath0 = textPathMap.get(childTaskSn)
                    def rawText0 = FileUtils.loadJson(rawPath0)
                    def text = trueValueSet[0].text

                    def startOffset = 0
                    def endOffset = 0
                    def keyWord1 = "资产管理计划的运作方式"
                    if(text.contains(keyWord1)){
                        startOffset = text.indexOf(keyWord1) + keyWord1.length()
                        endOffset = -1
                    }

                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText0)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }


        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = "其他"
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

        return trueValueSet
    }


}
